# -*- coding: utf-8 -*-


import mod.server.extraServerApi as serverApi
from mod.common.minecraftEnum import *
import nuoyanlib_server as nyl
from .common.constants import *


def neteaseShopEvent(func):
    return nyl.event(namespace="neteaseShop", system_name="neteaseShopDev")(func)


class HackerBuyer(nyl.NuoyanServerSystem):
    def __init__(self, namespace, systemName, ss):
        super(HackerBuyer, self).__init__(namespace, systemName)
        self.ss = ss
        self.__neteaseShopSys = None

    # ========================================= Engine Event Callback ==================================================

    @neteaseShopEvent
    def ServerShipItemsEvent(self, args):
        """
        插件自动会查询玩家还没发货的订单，然后触发此事件。
        参数：
            uid: int, 玩家uid
            entities: list, 玩家订单列表
            code: int, 返回码
            message: str, 返回信息
        """
        uid = args['uid']
        entities = args['entities']
        # 科学家发货
        successData = {'uid': uid, 'entities': []}
        print "-=" * 200
        print "好消息！发货了！"
        print uid, entities
        for entity in entities:
            cmd = entity['cmd']
            if cmd == HACKER_CMD:
                print cmd
                self.ss.AddPlayerHackerTime(uid)
                successData['entities'].append(entity)
        self.neteaseShopSys.ShipOrderSuccess(successData)

    @neteaseShopEvent
    def CheckItemResultEvent(self, args):
        """
        当调用CheckItem后，插件会查询玩家是否购买过传进来的商品，然后触发此事件。
        参数：
            uid: int, 玩家uid
            item_id: int, 商品id
            has_buy: bool, 是否购买商品
        """
        itemId = args['item_id']
        if itemId == HACKER_GOOD_ID:
            del args['item_id']
            self.BroadcastEvent(EVENT_CHECK_HACKER_RESULT, args)

    # =========================================== Basic Function =======================================================

    def CheckHacker(self, uid):
        """
        检查指定玩家是否购买科学家。检查完后会触发CheckHackerResultEvent，返回购买结果。
        """
        self.neteaseShopSys.CheckItem({'item_id': HACKER_GOOD_ID, 'uid': uid})

    @property
    def neteaseShopSys(self):
        if not self.__neteaseShopSys:
            self.__neteaseShopSys = serverApi.GetSystem("neteaseShop", "neteaseShopDev")
        return self.__neteaseShopSys

























